package ruc.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class Pval {
    public static int check(long val) {
        if (val <= 1) {
            return 1;
        } else if (val <= 2) {
            return 2;
        } else if (val <= 4) {
            return 3;
        } else if (val <= 8) {
            return 4;
        } else if (val <= 16) {
            return 5;
        } else if (val <= 32) {
            return 6;
        } else if (val <= 64) {
            return 7;
        } else if (val <= 128) {
            return 8;
        } else if (val <= 256) {
            return 9;
        } else if (val <= 512) {
            return 10;
        } else if (val <= 1024) {
            return 11;
        } else if (val <= 2048) {
            return 12;
        } else if (val <= 4096) {
            return 13;
        }
        return 14;
    }


    public static long calPVal(HashMap<Integer, List<Long>> m, int p_val) {
        int tol = p_val * 100;
        int idx = 14;
        while(tol > 0) {
            if (m.get(idx).size() > tol) {
                break;
            } else {
                tol -= m.get(idx).size();
                idx--;
            }
        }

        Random ran = new Random();
        int len = m.get(idx).size();
        return m.get(idx).get(ran.nextInt(len));
    }
}
